fix(json_schema_to_type): add allOf and oneOf handling#4019
Conversation
Handle allOf (intersection) and oneOf (exclusive union) schemas in json_schema_to_type(). Previously both silently returned Any. oneOf: produces a Union type (similar to anyOf) allOf: tries to merge object types, falls back to Union if not possible Fixes: #3839
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a02b6165fb
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if dc_field and dc_field.default is not DataclassField.MISSING: | ||
| merged_fields[field_name] = (field_type, field(default=dc_field.default)) | ||
| elif dc_field and dc_field.default_factory is not DataclassField.MISSING: | ||
| merged_fields[field_name] = (field_type, field(default_factory=dc_field.default_factory)) |
There was a problem hiding this comment.
Use dataclasses.MISSING for merged field defaults
Checking dc_field.default and dc_field.default_factory against DataclassField.MISSING raises AttributeError because Field has no MISSING attribute. Any allOf schema that reaches _merge_object_types() with dataclass-backed object branches will crash during type generation instead of returning a merged type.
Useful? React with 👍 / 👎.
| types = [t for t in types if t is not type(None)] | ||
| if len(types) == 0: | ||
| return type(None) | ||
| elif len(types) == 1: | ||
| return types[0] |
There was a problem hiding this comment.
Preserve null option when converting oneOf to a union
The oneOf branch drops type(None) and never reintroduces it, so schemas like {"oneOf": [{"type": "string"}, {"type": "null"}]} are converted to str instead of str | None. This rejects valid null values and produces an inaccurate Python type for nullable oneOf schemas.
Useful? React with 👍 / 👎.
|
Thanks for the contribution! This overlaps with #3840, which addresses the same gap in Generated by Claude Code |
|
Sorry, this was meant to be closed. the P1/P2 are not the issue (and, while a PR is open, should always be pushed to the original, not a new PR) |
Summary
Why
Issue #3839: json_schema_to_type() silently returns Any for both allOf and oneOf schemas, causing type information loss.
Validation